<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 2018/6/21
 * Time: 下午7:30
 */

namespace app\mall\controller;


use think\Request;

class Goods extends Base
{
    /**
     * @purpose 获取随机商品
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function rand ()
    {
        return \app\mall\logic\Goods::rand();
    }
    /**
     * @purpose 通过商品ID获取该商品相关的商品
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function relevantItemByGoodsId (Request $request)
    {
        $goods_id   = $request->get('goods_id');
        return \app\mall\logic\Goods::relevantItemByGoodsId($goods_id);
    }
    /**
     * @purpose 获取商品列表
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function item (Request $request)
    {
        $category_id    = $request->get('category_id', 0);
        $brand_id       = $request->get('brand_id', 0);
        $sort           = $request->get('sort',1);
        $page           = $request->get('page',1);
        $size           = $request->get('size',1);
        return \app\mall\logic\Goods::items($page, $size, $category_id, $brand_id, $sort);
    }
    /**
     * @purpose 搜索商品
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function search (Request $request)
    {
        $keywords       = $request->get('kw');
        $page           = $request->get('page', 1);
        $size           = $request->get('size', 20);
        return \app\mall\logic\Goods::search($keywords, $page, $size);
    }

    /**
     * @purpose 获取商品详情
     * @param Request $request
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail (Request $request)
    {
        $goods_id       = $request->get('id');
        return \app\mall\logic\Goods::detail($goods_id, self::$token);
    }

    /**
     * @purpose 根据商品编号获取指定属性的商品信息
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function attach (Request $request)
    {
        $number         = $request->get('number');
        $goods_id       = $request->get('goods_id');
        return \app\mall\logic\Goods::attach($number, $goods_id);
    }
}